package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.SetmealDto;
import com.itheima.reggie.entitry.Setmeal;
import com.itheima.reggie.entitry.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Transactional
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    SetmealService setmealService;
    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    CategoryService categoryService;


    //添加套餐
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
    public R<String> insert(@RequestBody SetmealDto setmealDto) {

        setmealService.insertByDto(setmealDto);

        return R.success("保存套餐成功！！");
    }


    //
    @GetMapping("/page")
    public R<Page> selectSetmeal(Integer page, Integer pageSize, String name) {

        Page<Setmeal> Ipage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);

        Page<Setmeal> page1 = setmealService.page(Ipage, lqw);
        List<SetmealDto> list = new ArrayList<>();
        for (Setmeal record : page1.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);


            setmealDto.setCategoryName(categoryService.getById(setmealDto.getCategoryId()).getName());
            list.add(setmealDto);
        }

        Page<SetmealDto> resultPage = new Page<>();
        resultPage.setRecords(list);
        resultPage.setTotal(page1.getTotal());


        return R.success(resultPage);
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
    public R<String> delelt(Long[] ids) {

        for (Long id : ids) {

            LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Setmeal::getStatus, 0).eq(Setmeal::getId, id);

            if (setmealService.getOne(lqw) == null) {
                return R.error("有套餐尚在启用，删除失败！");
            }

            LambdaQueryWrapper<SetmealDish> lqwDish = new LambdaQueryWrapper<>();
            lqwDish.eq(SetmealDish::getSetmealId, id);
            setmealDishService.remove(lqwDish);
            setmealService.remove(lqw);
        }
        return R.success("套餐删除成功！");
    }

    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable int status, Long[] ids) {


        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);

        for (Long id : ids) {
            setmeal.setId(id);
            setmealService.updateById(setmeal);
        }
        return R.success("操作成功！");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> selectById(@PathVariable Long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = setmealService.getById(id);
        BeanUtils.copyProperties(setmeal, setmealDto);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, id);
        setmealDto.setSetmealDishes(setmealDishService.list(lqw));
        return R.success(setmealDto);

    }

    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
    public R updateSetmeal(@RequestBody SetmealDto setmealDto) {

        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDto, setmeal);

        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();

        lqw.eq(SetmealDish::getSetmealId, setmealDto.getId());

        setmealDishService.remove(lqw);

        setmealService.updateById(setmeal);

        for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishService.save(setmealDish);
        }

        return R.success("修改套餐成功！");
    }

    /**
     * 根据套餐分类id查询套餐
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#categoryId + '_' + #status")
    public R<List<Setmeal>> selectDishByCat(Long categoryId, Integer status){
        //条件构建器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,status);
        lqw.orderByDesc(Setmeal::getUpdateTime);


        //查询
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);

    }
}
